'use strict';
module.exports = {
	/**
	 * 激活订单
	 * @url client/user/kh/orderActivate 前端调用的url参数地址
	 * data 请求参数 说明
	 * @param {String} client 客户端
	 * @param {String} _id	订单ID
	 * @param {String} order_no  订单编号
	 * @param {String} activate_code  激活码
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: "" };
		// 业务逻辑开始-----------------------------------------------------------
		let { client, _id, order_no, activate_code } = data;
		
		let dbName = "db-order";
		
		if(!vk.pubfn.isNull(_id) && !vk.pubfn.isNull(order_no) && !vk.pubfn.isNull(client) && !vk.pubfn.isNull(activate_code)) {
			let dbRes = await vk.baseDao.findById({
				dbName,
				id: _id
			})
			if(dbRes.activate_code.localeCompare(activate_code) === 0) {
				res["data"] = await vk.baseDao.updateAndReturn({
					dbName,
					whereJson: {
						client: client,
						_id: _id,
						order_no: order_no
					},
					dataJson: {
						status: 2
					}
				})
			} else {
				return {code: -1, msg: "激活码错误，请检查激活码是否正确！"}
			}
			if(!res.data) {
				return {code: -1, msg: "激活失败，订单参数错误/不存在！"}
			}
		} else {
			return {code: -1, msg: "激活失败，缺少参数！"}
		}

		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
